package com.service.impl;

import com.bean.entity.Client;
import com.bean.vo.ClientVo;
import com.bean.vo.PageAssistant;
import com.dao.ClientMapper;
import com.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {

    /*自动注入*/
    @Autowired
    ClientMapper clientMapper;

    @Override
    public PageAssistant<ClientVo> queryAll(ClientVo clientVo, PageAssistant<ClientVo> pa) {
        //查询总记录数
        pa.setCount(clientMapper.selectCount(clientVo));
        //设置分页
        clientVo.setPageSize(pa.getPageSize());
        clientVo.setCurrPage(pa.getCurrPage());
        clientVo.setStartCode(pa.getStartCode());
        //设置页面内容
        pa.setItems(clientMapper.selectByPage(clientVo));
        return pa;
    }

    @Override
    public ClientVo query(Client client) throws Exception {
        List<ClientVo> clients = clientMapper.select(client);
        if(clients.size() == 1){
            return clients.get(0);
        }else{
            throw new Exception("抱歉，未能查询到相关信息");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(Client client) throws Exception {
        //执行
        int i = clientMapper.insert(client);
        if(i == 1){
            return 1;
        }else{
            throw new Exception("新增失败!");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(Client client) throws Exception {
        //执行
        int i = clientMapper.delete(client);
        if(i == 1){
            return 1;
        }else{
            throw new Exception("删除失败!");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modify(Client client) throws Exception {
        //执行
        int i = clientMapper.update(client);
        if(i == 1){
            return 1;
        }else{
            throw new Exception("修改失败!");
        }
    }
}
